package com.itheima.controller;


import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.UmsAdmin;
import com.itheima.manager.UmsAdminManager;
import com.itheima.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UmsAdminController {

    @Autowired
    UmsAdminManager umsAdminManager;

    //登陆
    @PostMapping("/login")
    public R login(@RequestBody Map<String ,String> map){
        return umsAdminManager.login(map);
    }

    //获取当前登录用户信息
    @GetMapping("/info")
    public R getAdminInfo(){
        return umsAdminManager.getAdminInfo();
    }

    //登出
    @PostMapping("/logout")
    public R logout(@RequestHeader("Authorization")String token){
        return umsAdminManager.logout(token);
    }

    //刷新token
    @GetMapping("/refreshToken")
    public R refreshToken(@RequestHeader("Authorization")String token){
        return umsAdminManager.refreshToken(token);
    }

    //根据用户名或姓名分页获取用户列表
    @GetMapping("/list")
    public R getList(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "keyword",required = false) String keyword
    ){
        return umsAdminManager.getList(pageNum,pageSize,keyword);
    }

    // 用户注册
    @PostMapping("/register")
    public R userRegister(@RequestBody Map<String,String> map){
        String email = map.get("email");
        String icon = map.get("icon");
        String nickName = map.get("nickName");
        String note = map.get("note");
        String password = map.get("password");
        String username = map.get("username");
        UmsAdmin umsAdmin = new UmsAdmin();
        umsAdmin.setEmail(email);
        umsAdmin.setIcon(icon);
        umsAdmin.setNickName(nickName);
        umsAdmin.setNote(note);
        umsAdmin.setPassword(SecureUtil.md5(password));
        umsAdmin.setUsername(username);

        // 创建时间
        umsAdmin.setCreateTime(LocalDateTime.now()); // 时间格式为yyyy-MM-dd HH:mm:ss
        return umsAdminManager.userRegister(umsAdmin);
    }

    // 修改指定用户信息
    @PostMapping("/update/{id}")
    public R updateMessage(@PathVariable Integer id,@RequestBody UmsAdmin umsAdmin){
        return umsAdminManager.updateMessage(id,umsAdmin);
    }

    //删除指定用户信息
    @PostMapping("/delete/{id}")
    public R deleteUserMessage(@PathVariable Long id){
        return umsAdminManager.deleteUserMessage(id);
    }

    // 修改账号状态
    @PostMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Long id){
        return umsAdminManager.updateStatus(id);
    }
}
